using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using IGM.DataProvider.Entites;
using IGM.Framework.Commons;

namespace IGM.WcfService
{
    public partial class DomainService
    {
        #region IDomainService Members

        public ObservableCollection<DataManagement> GetLecturers()
        {
            return new ObservableCollection<DataManagement>(
                    _context.GetObjectSet<Lecturer>().Cast<DataManagement>().ToList());
        }

        public ObservableCollection<DataManagement> GetLecturersByMajorId(int majorId)
        {
            Expression<Func<Lecturer, bool>> expression = s => s.IdMajor == majorId;
            return new ObservableCollection<DataManagement>(
                      _context.GetObjectSet(expression).Cast<DataManagement>().ToList());
        }        

        public void InsertLecturers(List<DataManagement> entities)
        {
            foreach (var entity in entities)
            {
                _context.InsertObject(entity.EntitySetName, (Lecturer) entity);
            }
            _context.SaveChanges();
        }

        public void UpdateLecturers(List<DataManagement> entities)
        {
            foreach (var entity in entities)
            {
                var id = entity.Id;
                Expression<Func<Lecturer, bool>> exp = l => l.Id == id;
                var updateItem = _context.GetObject(exp);
                var orginal = (Lecturer) entity;
                Utils.UpdateProperties(orginal, ref updateItem, null);
            }
            _context.SaveChanges();
        }

        public void DeleteLecturers(List<DataManagement> entities)
        {
            foreach (var entity in entities)
            {
                var id = entity.Id;
                Expression<Func<Lecturer, bool>> exp = e => e.Id == id;
                var deleteItem = _context.GetObject(exp);
                deleteItem.Status = false;
            }
            _context.SaveChanges();
        }

        #endregion
    }
}